﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using FONCREAGROINTEGRAL.Models;

namespace FONCREAGROINTEGRAL.Controllers
{
    public class tipodocumentoController : Controller
    {
        public SistIntFoncreagroEntities entidades = new SistIntFoncreagroEntities();

        //
        // GET: /tipodocumento/
        public ActionResult Venta()
        {
            var query = entidades.TIPODOCUMENTOes.Where(td => td.IdTipoDocumento == 2 || td.IdTipoDocumento == 4);

            var result = (from pc in query.ToList()
                          select new
                          {
                              IdTipoDocumento = pc.IdTipoDocumento,
                              Codigo = pc.Codigo,
                              NomTipoDocumento = pc.Nombre,
                          }).ToList();

            return Json(new { grid = result, total = query.Count() }, JsonRequestBehavior.AllowGet);
        }

        //
        // GET: /tipodocumento/
        public ActionResult IndexById(int id)
        {
            var query = entidades.TIPODOCUMENTOes.Where(td => td.IdTipoDocumento == id);

            var result = (from pc in query.ToList()
                          select new
                          {
                              IdTipoDocumento = pc.IdTipoDocumento,
                              Codigo = pc.Codigo,
                              NomTipoDocumento = pc.Nombre,
                          }).ToList();

            return Json(new { grid = result, total = query.Count() }, JsonRequestBehavior.AllowGet);
        }

        public ActionResult Index()
        {
            int start = Convert.ToInt32(Request.QueryString["start"]);
            int limit = Convert.ToInt32(Request.QueryString["limit"]);

            string filtro = Convert.ToString(Request.QueryString["query"]);

            var query = entidades.TIPODOCUMENTOes.Where(td => td.IdTipoDocumento >= 1);

            if (!String.IsNullOrEmpty(filtro))
            {
                query = query.Where(a => (a.Nombre).ToUpper().Contains(filtro.ToUpper())
                || (a.Codigo).ToUpper().Contains(filtro.ToUpper()));
            }
            var result = (from pc in query.OrderBy(p => p.Nombre).Skip(start).Take(limit).ToList()
                          select new
                          {
                              IdTipoDocumento = pc.IdTipoDocumento,
                              Codigo = pc.Codigo,
                              NomTipoDocumento = pc.Nombre,
                          }).ToList();

            return Json(new { grid = result, total = query.Count() }, JsonRequestBehavior.AllowGet);
        }

        //
        // GET: /tipodocumento/Details/5

        public ActionResult Details(int id)
        {
            return View();
        }

        //
        // GET: /tipodocumento/Create

        public ActionResult Create()
        {
            return View();
        } 

        //
        // POST: /tipodocumento/Create

        [HttpPost]
        public ActionResult Create(FormCollection collection)
        {
            try
            {
                // TODO: Add insert logic here

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
        
        //
        // GET: /tipodocumento/Edit/5
 
        public ActionResult Edit(int id)
        {
            return View();
        }

        //
        // POST: /tipodocumento/Edit/5

        [HttpPost]
        public ActionResult Edit(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add update logic here
 
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        //
        // GET: /tipodocumento/Delete/5
 
        public ActionResult Delete(int id)
        {
            return View();
        }

        //
        // POST: /tipodocumento/Delete/5

        [HttpPost]
        public ActionResult Delete(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add delete logic here
 
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
    }
}
